package com.poetry.vup.common.util;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.poetry.vup.common.result.page.BasePage;
import com.poetry.vup.common.result.page.PageParam;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页工具类
 *
 * @author mileswan
 */
public final class PageUtils {
	/**
	 * 分页对象转换至Mybatis-plus的分页对象
	 * @param <T>
	 * @param page
	 * @param clazz
	 * @return
	 */
	public static <T> IPage<T> convertPage(IPage<?> page, Class<T> clazz) {

		IPage<T> resultPage = new Page<T>();
		resultPage.setRecords(JacksonUtil.listToListObject(page.getRecords(), clazz));
		resultPage.setCurrent(page.getCurrent());
		resultPage.setPages(page.getPages());
		resultPage.setSize(page.getSize());
		resultPage.setTotal(page.getTotal());
		return resultPage;

	}
	
	/**
	 * 分页对象转换为自定义的BasePage
	 * @param <T>
	 * @param page
	 * @param clazz
	 * @return
	 */
	public static <T> BasePage<T> convertToBasePage(IPage<?> page, Class<T> clazz) {

		BasePage<T> resultPage = new BasePage<T>();
		resultPage.setList(JacksonUtil.listToListObject(page.getRecords(), clazz));
		resultPage.setPageNum((int)page.getCurrent());
		resultPage.setTotalPage((int)page.getPages());
		resultPage.setPageSize((int)page.getSize());
		resultPage.setTotal(page.getTotal());
		return resultPage;

	}
	
	/**
	 * 转换BasePage的数据对象
	 * @param <T>
	 * @param page
	 * @param clazz
	 * @return
	 */
	public static <T> BasePage<T> convertBasePage(BasePage<?> page, Class<T> clazz) {

		BasePage<T> resultPage = new BasePage<T>();
		resultPage.setList(JacksonUtil.listToListObject(page.getList(), clazz));
		resultPage.setPageNum(page.getPageNum());
		resultPage.setTotalPage(page.getTotalPage());
		resultPage.setPageSize(page.getPageSize());
		resultPage.setTotal(page.getTotal());
		return resultPage;

	}
	
	/**
	 * 通过data生成BasePage
	 * @param <T>
	 * @param data
	 * @param clazz
	 * @param pageParam
	 * @param totalCount
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> BasePage<T> buildBasePage(List<?> data, Class<T> clazz, PageParam pageParam, long totalCount) {
		BasePage<T> resultPage = new BasePage<T>();
		
		if(data == null || data.size() < 1) {
			resultPage.setList(new ArrayList<T>());
		}else if(data.get(0).getClass() == clazz) {
			resultPage.setList((List<T>)data);
		}else {
			resultPage.setList(JacksonUtil.listToListObject(data, clazz));
		}
		
		resultPage.setPageNum(pageParam.getPageNum());
		resultPage.setTotalPage(getTotalPage(totalCount, pageParam.getPageSize()));
		resultPage.setPageSize(pageParam.getPageSize());
		resultPage.setTotal(totalCount);
		return resultPage;
	}
	
	public static <T> BasePage<T> buildBasePage(List<T> data, PageParam pageParam, long totalCount) {
		BasePage<T> resultPage = new BasePage<T>();
		
		if(data == null || data.size() < 1) {
			resultPage.setList(new ArrayList<T>());
		}else {
			resultPage.setList(data);
		}
		
		resultPage.setPageNum(pageParam.getPageNum());
		resultPage.setTotalPage(getTotalPage(totalCount, pageParam.getPageSize()));
		resultPage.setPageSize(pageParam.getPageSize());
		resultPage.setTotal(totalCount);
		return resultPage;
	}

	/**
	 * 计算查询偏移量
	 * @param pageParam
	 * @return
	 */
	public static int getSelectOffSet(PageParam pageParam) {
		int offset = (pageParam.getPageNum() - 1) * pageParam.getPageSize();
        if (offset < 0) {
            offset = 0;
        }
        return offset;
	}
	
	/**
	 * 计算总页数
	 * @param totalCount
	 * @param pageSize
	 * @return
	 */
	public static int getTotalPage(long totalCount, int pageSize) {
		int totalPage = 0;
        if (pageSize != 0) {
        	if(totalCount % pageSize == 0) {
        		totalPage = (int) (Math.floor(totalCount / pageSize));
        	}else {
        		totalPage = (int) (Math.floor(totalCount / pageSize) + 1);
        	}
        }
        return totalPage;
	}
	
}
